﻿
using System;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using System.Threading.Tasks;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Surging.Core.Common;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using System.Collections.Generic;
using System.Linq;

namespace Dora.Services.AdminUser.Repository
{

    /// <summary>
    /// 大社产品类型/目的地保险产品绑定仓储层
    /// </summary>
    public class OrgInsuranceSettingRepository : SqlSugarRepository<OrgInsuranceSettingEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgInsuranceSettingRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        /// <summary>
        /// 逻辑删除或恢复
        /// </summary>
        /// <param name="orgInsuranceSettingEntities"></param>
        /// <param name="isDelete"></param>
        /// <returns></returns>
        public async Task<int> UpZOrgSeting(List<OrgInsuranceSettingEntity> orgInsuranceSettingEntities, Boolean isDelete)
        {
            for (int i = 0; i < orgInsuranceSettingEntities.Count; i++)
            {
                orgInsuranceSettingEntities[i].IsDeleted = isDelete;
            }
            return await _unitOfWork.ModifyClient.Updateable<OrgInsuranceSettingEntity>(orgInsuranceSettingEntities).ExecuteCommandAsync();
        }
        /// <summary>
        /// 彻底删除
        /// </summary>
        /// <param name="orgInsuranceSettingEntities"></param>
        /// <returns></returns>
        public async Task<int> ClearZOrgSeting(List<OrgInsuranceSettingEntity> orgInsuranceSettingEntities)
        {
            return await _unitOfWork.ModifyClient.Deleteable<OrgInsuranceSettingEntity>(orgInsuranceSettingEntities).ExecuteCommandAsync();
        }

        /// <summary>
        /// 批量新增及修改
        /// </summary>
        /// <param name="orgInsuranceSettingEntities"></param>
        /// <returns></returns>
        public async Task<bool> BatchSaveOrgInsuranceSettings(List<OrgInsuranceSettingEntity> orgInsuranceSettingEntities)
        {
            List<OrgInsuranceSettingEntity> InList = new List<OrgInsuranceSettingEntity>();
            List<OrgInsuranceSettingEntity> UpList = new List<OrgInsuranceSettingEntity>();

            for (int i = 0; i < orgInsuranceSettingEntities.Count; i++)
            {
                if (orgInsuranceSettingEntities[i].Id > 0)
                {
                    UpList.Add(orgInsuranceSettingEntities[i]);
                }
                else
                {
                    List<OrgInsuranceSettingEntity> hasEntity = _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity>().Where(t => t.OrgId == orgInsuranceSettingEntities[i].OrgId && t.BindType == orgInsuranceSettingEntities[i].BindType && t.BindId == orgInsuranceSettingEntities[i].BindId).ToList();
                    if (hasEntity!=null&& hasEntity.Count>0)
                    {
                        orgInsuranceSettingEntities[i].Id = hasEntity[0].Id;
                        UpList.Add(orgInsuranceSettingEntities[i]);
                    }
                    else
                    {
                        InList.Add(orgInsuranceSettingEntities[i]);
                    }
                    
                }
            }
            var modifyClient = _unitOfWork.ModifyClient;
            bool result = true;
            try
            {
                modifyClient.BeginTran();
                if (UpList.Count > 0)
                {
                    result &= modifyClient.Updateable(UpList).ExecuteCommand() > 0;
                }
                if (InList.Count > 0)
                {
                    result &= modifyClient.Insertable(InList).ExecuteCommand() > 0;
                }

                if (result)
                {
                    modifyClient.CommitTran();
                }
                else
                {
                    modifyClient.RollbackTran();
                }
                return result;
            }
            catch (Exception ex)
            {
                modifyClient.RollbackTran();
                Console.WriteLine("BatchSaveOrgInsuranceSettings:" + ex.Message);
                return result;
            }
        }
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<OrgInsuranceSettingEntity>> GetOrgInsuranceSettingPageList(OrgInsuranceSettingQueryDto input)
        {
            PageDataList<OrgInsuranceSettingEntity> PageDataList = new PageDataList<OrgInsuranceSettingEntity>();
            if (input.Id > 0)
            {
                var pagePredict = _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity, OrganizationEntity>((ois, o)
                 => new object[] {JoinType.Inner, ois.OrgId==o.Id
                 })
                 .Where(ois => ois.Id == input.Id)
                 .Where(ois => ois.IsDeleted == false)
                 .OrderBy(ois => ois.Id)
                 .Select((ois, o)
                            => new OrgInsuranceSettingEntity
                            {
                                Id = ois.Id,
                                BindId = ois.BindId,
                                BindName = ois.BindName,
                                BindType = ois.BindType,
                                CreatedTime = ois.CreatedTime,
                                CreatedUserId = ois.CreatedUserId,
                                CreatedUserName = ois.CreatedUserName,
                                InsuranceOrgProId = ois.InsuranceOrgProId,
                                InsuranceOrgProName = ois.InsuranceOrgProName,
                                InsuranceSupplierId = ois.InsuranceSupplierId,
                                IsDeleted = ois.IsDeleted,
                                IsEnable = ois.IsEnable,
                                LevelPath = ois.LevelPath,
                                OrgId = ois.OrgId,
                                OrgLevel = ois.OrgLevel,
                                TopOrgId = ois.TopOrgId,
                                UpdatedTime = ois.UpdatedTime,
                                UpdatedUserId = ois.UpdatedUserId,
                                UpdatedUserName = ois.UpdatedUserName,
                                //Name = o.Name,
                                //ShortName = o.ShortName
                            }).OrderBy(ois => ois.Id, OrderByType.Desc);

                var totalCount = await pagePredict.CountAsync();
                var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                    .ToPageListAsync(input.PageIndex, input.PageSize);
                var page = new PageDataList<OrgInsuranceSettingEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
                return page;
            }
            else
            {
                var pagePredict = _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity, OrganizationEntity>((ois, o)
                 => new object[] {JoinType.Inner, ois.OrgId==o.Id
                 })
                 .Where(ois => ois.OrgId == input.OrgId)
                 .WhereIF(input.BindId > 0, ois => ois.BindId == input.BindId)
                 .WhereIF(input.BindType > 0, ois => ois.BindType == input.BindType)
                 .Where(ois => ois.IsDeleted == false)
                 .OrderBy(ois => ois.Id)
                 .Select((ois, o)
                            => new OrgInsuranceSettingEntity
                            {
                                Id = ois.Id,
                                BindId = ois.BindId,
                                BindName = ois.BindName,
                                BindType = ois.BindType,
                                CreatedTime = ois.CreatedTime,
                                CreatedUserId = ois.CreatedUserId,
                                CreatedUserName = ois.CreatedUserName,
                                InsuranceOrgProId = ois.InsuranceOrgProId,
                                InsuranceOrgProName = ois.InsuranceOrgProName,
                                InsuranceSupplierId = ois.InsuranceSupplierId,
                                IsDeleted = ois.IsDeleted,
                                IsEnable = ois.IsEnable,
                                LevelPath = ois.LevelPath,
                                OrgId = ois.OrgId,
                                OrgLevel = ois.OrgLevel,
                                TopOrgId = ois.TopOrgId,
                                UpdatedTime = ois.UpdatedTime,
                                UpdatedUserId = ois.UpdatedUserId,
                                UpdatedUserName = ois.UpdatedUserName,
                                //Name = o.Name,
                                //ShortName = o.ShortName
                            }).OrderBy(ois => ois.Id, OrderByType.Desc);

            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<OrgInsuranceSettingEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<OrgInsuranceSettingEntity>> GetOrgInsuranceSettingAll(OrgInsuranceSettingQueryDto input)
        {

            if (input.Id > 0)
            {
                var pagePredict = await _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity, OrganizationEntity>((ois, o)
                 => new object[] {JoinType.Inner, ois.OrgId==o.Id
                 })
                 .Where(ois => ois.Id == input.Id)
                 .Where(ois => ois.IsDeleted == false)
                 .OrderBy(ois => ois.Id)
                 .Select((ois, o)
                            => new OrgInsuranceSettingEntity
                            {
                                Id = ois.Id,
                                BindId = ois.BindId,
                                BindName = ois.BindName,
                                BindType = ois.BindType,
                                CreatedTime = ois.CreatedTime,
                                CreatedUserId = ois.CreatedUserId,
                                CreatedUserName = ois.CreatedUserName,
                                InsuranceOrgProId = ois.InsuranceOrgProId,
                                InsuranceOrgProName = ois.InsuranceOrgProName,
                                InsuranceSupplierId = ois.InsuranceSupplierId,
                                IsDeleted = ois.IsDeleted,
                                IsEnable = ois.IsEnable,
                                LevelPath = ois.LevelPath,
                                OrgId = ois.OrgId,
                                OrgLevel = ois.OrgLevel,
                                TopOrgId = ois.TopOrgId,
                                UpdatedTime = ois.UpdatedTime,
                                UpdatedUserId = ois.UpdatedUserId,
                                UpdatedUserName = ois.UpdatedUserName,
                                SourceOrgId = ois.SourceOrgId
                                //Name = o.Name,
                                //ShortName = o.ShortName
                            }).OrderBy(ois => ois.Id, OrderByType.Desc).ToListAsync();
                return pagePredict;
            }
            else
            {
                var pagePredict = await _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity, OrganizationEntity>((ois, o)
                 => new object[] {JoinType.Inner, ois.OrgId==o.Id
                 })
                 .Where( ois => ois.OrgId == input.OrgId)
                 .WhereIF(input.BindId>0, ois => ois.BindId == input.BindId)
                 .WhereIF(input.BindType>0, ois => ois.BindType == input.BindType)
                 .Where(ois => ois.IsDeleted == false)
                 .OrderBy(ois => ois.Id)
                 .Select((ois, o)
                            => new OrgInsuranceSettingEntity
                            {
                                Id = ois.Id,
                                BindId = ois.BindId,
                                BindName = ois.BindName,
                                BindType = ois.BindType,
                                CreatedTime = ois.CreatedTime,
                                CreatedUserId = ois.CreatedUserId,
                                CreatedUserName = ois.CreatedUserName,
                                InsuranceOrgProId = ois.InsuranceOrgProId,
                                InsuranceOrgProName = ois.InsuranceOrgProName,
                                InsuranceSupplierId = ois.InsuranceSupplierId,
                                IsDeleted = ois.IsDeleted,
                                IsEnable = ois.IsEnable,
                                LevelPath = ois.LevelPath,
                                OrgId = ois.OrgId,
                                OrgLevel = ois.OrgLevel,
                                TopOrgId = ois.TopOrgId,
                                UpdatedTime = ois.UpdatedTime,
                                UpdatedUserId = ois.UpdatedUserId,
                                UpdatedUserName = ois.UpdatedUserName,
                                SourceOrgId = ois.SourceOrgId
                                //Name = o.Name,
                                //ShortName = o.ShortName
                            }).OrderBy(ois => ois.Id, OrderByType.Desc).ToListAsync();
                return pagePredict;
            }
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<OrgInsuranceSettingEntity>> GetOrgInsuranceSettingAllByLevePath(String levelPath)
        {
            return await _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity>().Where(t => t.LevelPath.StartsWith(levelPath)).OrderBy(t=>t.LevelPath).ToListAsync();
        }

            //       /// <summary>
            //       /// 批量获取默认集合
            //       /// </summary>
            //       /// <param name="orgInsuranceDefaultEntities"></param>
            //       /// <returns></returns>
            //       public async Task<List<OrgInsuranceDefaultEntity>> GetOrgInsuranceDefaultEntities(List<OrgInsuranceDefaultEntity> orgInsuranceDefaultEntities)
            //       {
            //           for (int i = 0; orgInsuranceDefaultEntities != null && i < orgInsuranceDefaultEntities.Count; i++)
            //           {
            //               var pagePredict = await _unitOfWork.QueryClient.Queryable<OrgInsuranceSettingEntity, OrgInsuranceSupplierEntity, OrgInsuranceSupplierAreaEntity>((o,s,a)
            //=> new object[] {JoinType.Inner, o.Id == s.SettingId ,JoinType.Left ,s.Id == a.ExcludeSupplierId
            //}).wh


            //           }
            //       }
        }
}